import app from './http';
import renderer from './renderer';
import createStore from './createStore';
import { matchRoutes } from 'react-router-config';
import routes from '../share/routes';

app.get('*', (req, res) => {
  const promises = [];
  const store = createStore();

  // 匹配路由
  matchRoutes(routes, req.path).forEach(({ route }) => {
    // 调用loadData，并添加到promises中
    if (route.loadData) {
      promises.push(route.loadData(store));
    }
  });

  // 等待promises中的请求全部响应时，渲染拼接html并返回
  Promise.all(promises).then(() => {
    res.send(renderer(req, store));
  });
});